package org.gary.product.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.gary.base.model.Category;
import org.gary.base.model.Image;
import org.gary.core.enums.CategoryType;

/**
 * 产品
 * 
 * @author lengreen
 * 
 */
@Entity
public class Product implements lengreen.core.interfaces.Entity {
	/**
	 * 在记录日志中需要用到该方法
	 */
	@Override
	public String toString() {
		return "Product [createDate=" + createDate + ", id=" + id + ", 商品型号="
				+ model + ", 商品名称=" + name + ", 商品是否可见=" + visible
				+ ", 语言=" + lang + "]";
	}

	private int id;
	private boolean visible;
	private String name;// 产品名字
	private String model;// 产品型号
	private String notes;// 产品备注
	private String lang;//语言 type in enum LangType

	private Date createDate;

	private Set<Image> images = new HashSet<Image>();// 多对一，双向,产品图片，包括简介中的图片
	private Category category;// 一对一，单向所属类别

	public Product() {		
		setCreateDate(new Date());
		setVisible(true);
	}

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	@Lob
	public String getNotes() {
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	public Set<Image> getImages() {
		return images;
	}

	public void setImages(Set<Image> images) {
		this.images = images;
	}

	@OneToOne
	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		category.setWhoCategory(CategoryType.product.toString());
		this.category = category;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	public boolean isVisible() {
		return visible;
	}

	public void setLang(String lang) {
		this.lang = lang;
	}

	public String getLang() {
		return lang;
	}
	
	@Transient
	public int getLength(){
		return images.size();
	}
}
